/**
 * 
 */
package com.dhcc.cdbp.createindex;

import java.io.IOException;

import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.BytesRef;
import org.junit.Test;

import com.dhcc.cdbp.util.TestUtil;

/**
 * @author 谢进军
 *
 */
public class NumericFieldIndexTest {
	
	@Test public void testNumericFieldIndex() throws IOException{
		Directory directory = new RAMDirectory();
		IndexWriterConfig iwc = new IndexWriterConfig(new WhitespaceAnalyzer());
		iwc.setOpenMode(OpenMode.CREATE);
		IndexWriter writer = new IndexWriter(directory,iwc);
		for (int i = 0; i < 20; i++) {
			Document document = new Document();
			document.add(new NumericDocValuesField("number", i));
			writer.addDocument(document);
		}
		writer.commit();
		IndexReader reader = DirectoryReader.open(directory);
		IndexSearcher searcher = new IndexSearcher(reader);
		Term t = new Term("number", new BytesRef(1));
		Query query = new TermQuery(t);
		int hitCount =TestUtil.hitCount(searcher, query);
	   System.out.println(hitCount);
	   reader.close();
	   writer.close();
	}
}
